/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.elements;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.EnumIndexType;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.Arrays;

public class EnumForValueGenerator extends ClassMethodGenerator {
    private String enumClassName;
    private IGspCommonField field;

    public  EnumForValueGenerator(String enumClassName)
    {

        this.enumClassName = enumClassName;
    }
    public  EnumForValueGenerator(String enumClassName,IGspCommonField field)
    {
        this.enumClassName = enumClassName;
        this.field=field;
    }
    @Override
    protected Block buildMethodBody() {
//        Block block=ast.newBlock();
//        ReturnStatement returnStatement=ast.newReturnStatement();
//        ArrayAccess arrayAccess=ast.newArrayAccess();
//
//        MethodInvocation methodInvocation=ast.newMethodInvocation();
//        methodInvocation.setName(ast.newSimpleName("values"));
//        arrayAccess.setArray(methodInvocation);
//
//        arrayAccess.setIndex(ast.newSimpleName("value"));
//
//        returnStatement.setExpression(arrayAccess);
//        block.statements().add(returnStatement);
//        Block block=ast.newBlock();
//        MethodInvocation methodInvocation2 =ast.newMethodInvocation();
//        MethodInvocation methodInvocation3 =ast.newMethodInvocation();
//        methodInvocation3.setName(ast.newSimpleName("getMappings"));
//        methodInvocation2.setExpression(methodInvocation3);
//        methodInvocation2.setName(ast.newSimpleName("get"));
//        methodInvocation2.arguments().add(ast.newSimpleName("value"));
//        ReturnStatement returnStatement1=ast.newReturnStatement();
//        returnStatement1.setExpression(methodInvocation2);
//        block.statements().add(returnStatement1);
        //

        Block block=ast.newBlock();
        IfStatement ifStatement=ast.newIfStatement();
        PrefixExpression prefixExpression=ast.newPrefixExpression();
        prefixExpression.setOperator(PrefixExpression.Operator.NOT);

        MethodInvocation methodInvocation=ast.newMethodInvocation();

        MethodInvocation methodInvocation1=ast.newMethodInvocation();
        methodInvocation1.setName(ast.newSimpleName("getMappings"));
        Block block1=ast.newBlock();
        addImport(CefException.class.getTypeName());
        ThrowStatement throwStatement= GeneratorUtil.getThrowStatement(ast,"索引数组中不存在当前值：","value",true);
//        ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
//        classInstanceCreation.setType(ast.newSimpleType(ast.newSimpleName("RuntimeException")));
//        InfixExpression infixExpression=ast.newInfixExpression();
//        StringLiteral stringLiteral=ast.newStringLiteral();
//        stringLiteral.setLiteralValue("索引数组中不存在当前值：");
//
//        infixExpression.setLeftOperand(stringLiteral);
//        infixExpression.setOperator(InfixExpression.Operator.PLUS);
//        infixExpression.setRightOperand(ast.newSimpleName("value"));
//        classInstanceCreation.arguments().add(infixExpression);
//        throwStatement.setExpression(classInstanceCreation);
        block1.statements().add(throwStatement);

        ifStatement.setThenStatement(block1);

        block.statements().add(ifStatement);

        methodInvocation.setName(ast.newSimpleName("containsKey"));
        methodInvocation.arguments().add(ast.newSimpleName("value"));
        methodInvocation.setExpression(methodInvocation1);
        prefixExpression.setOperand(methodInvocation);

        ifStatement.setExpression(prefixExpression);


        MethodInvocation methodInvocation2 =ast.newMethodInvocation();
        MethodInvocation methodInvocation3 =ast.newMethodInvocation();
        methodInvocation3.setName(ast.newSimpleName("getMappings"));
        methodInvocation2.setExpression(methodInvocation3);
        methodInvocation2.setName(ast.newSimpleName("get"));
        methodInvocation2.arguments().add(ast.newSimpleName("value"));
        ReturnStatement returnStatement1=ast.newReturnStatement();
        returnStatement1.setExpression(methodInvocation2);
        block.statements().add(returnStatement1);
        return block;
    }

    @Override
    protected String getMethodName() {
        return "forValue";
    }

    @Override
    protected TypeInfo getReturnType() {
        return new TypeInfo(enumClassName);
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        return new ArrayList<>(Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD, Modifier.ModifierKeyword.STATIC_KEYWORD}));
    }

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        EnumIndexType enumIndexType = this.field.getEnumIndexType();
        ParameterInfo parameterInfo = new ParameterInfo();
        parameterInfo.setParamName("value");
        if (enumIndexType == EnumIndexType.String) {
            parameterInfo.setParamType(new TypeInfo(String.class));
        } else {

            parameterInfo.setParamType(new TypeInfo(Integer.class));
        }
        return new ArrayList(Arrays.asList(new ParameterInfo[] { parameterInfo }));
    }
}
